package com.donfu.web.controller.house;

import java.util.Date;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.donfu.common.annotation.Log;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.common.enums.BusinessType;
import com.donfu.system.domain.house.HouseDiary;
import com.donfu.system.service.house.IHouseDiaryService;
import com.donfu.common.utils.poi.ExcelUtil;
import com.donfu.common.core.page.TableDataInfo;
import org.springframework.web.util.HtmlUtils;

/**
 * 新居日志Controller
 * 
 * @author donfu
 * @date 2021-12-01
 */
@Api(tags = "后台-新居日志管理")
@RestController
@RequestMapping("/house/diary")
public class HouseDiaryController extends BaseController
{
    @Autowired
    private IHouseDiaryService houseDiaryService;

    /**
     * 查询新居日志列表
     */
    @PreAuthorize("@ss.hasPermi('house:diary:list')")
    @ApiOperation("列表查询")
    @GetMapping("/list")
    public TableDataInfo list(HouseDiary houseDiary)
    {
        startPage();
        List<HouseDiary> list = houseDiaryService.selectHouseDiaryList(houseDiary);
        return getDataTable(list);
    }

    /**
     * 导出新居日志列表
     */
    @PreAuthorize("@ss.hasPermi('house:diary:export')")
    @ApiOperation("导出")
    @Log(title = "新居日志", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HouseDiary houseDiary)
    {
        List<HouseDiary> list = houseDiaryService.selectHouseDiaryList(houseDiary);
        ExcelUtil<HouseDiary> util = new ExcelUtil<HouseDiary>(HouseDiary.class);
        return util.exportExcel(list, "新居日志数据");
    }

    /**
     * 获取新居日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('house:diary:query')")
    @ApiOperation("详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        HouseDiary houseDiary = houseDiaryService.selectHouseDiaryById(id);
        if (houseDiary!=null&&houseDiary.getContent()!=null) {
            houseDiary.setContent(HtmlUtils.htmlUnescape(houseDiary.getContent()));
        }
        return AjaxResult.success(houseDiary);
    }

    /**
     * 新增新居日志
     */
    @PreAuthorize("@ss.hasPermi('house:diary:add')")
    @ApiOperation("新增")
    @Log(title = "新居日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseDiary houseDiary)
    {
        houseDiary.setCreateDate(new Date());
        return toAjax(houseDiaryService.insertHouseDiary(houseDiary));
    }

    /**
     * 修改新居日志
     */
    @PreAuthorize("@ss.hasPermi('house:diary:edit')")
    @ApiOperation("修改")
    @Log(title = "新居日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseDiary houseDiary)
    {
        houseDiary.setUpdateDate(new Date());
        return toAjax(houseDiaryService.updateHouseDiary(houseDiary));
    }

    /**
     * 删除新居日志
     */
    @PreAuthorize("@ss.hasPermi('house:diary:remove')")
    @ApiOperation("删除")
    @Log(title = "新居日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(houseDiaryService.deleteHouseDiaryByIds(ids));
    }
}
